package com.dajie.pt.model;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.dajie.common.util.StringUtil;
import com.dajie.pt.entity.base.BaseEntity;
import com.dajie.pt.utils.Weight;

/**
 * 
 * @author panbing.wu
 * 
 */
public class InvitationApply extends BaseEntity
{

	private static final long serialVersionUID = 3544262706783848873L;

	private Integer uid;

	@Weight(value = 60)
	private String email;
	
	private String emailOld;

	@Weight(value = 60)
	private String mobile;

	private String mobileOld;

	private Integer invitationId;

	private String jid;

	private BasicInfo basicInfo;

	private PracticeInfo defaultPracticeInfo;

	private List<PracticeInfo> practiceInfos;

	private EduInfo defaultEduInfo;

	private List<EduInfo> eduInfos;

	private IntentionInfo intentionInfo;

	@Weight(value = 20)
	private String itskillsDesc;

	@Weight(value = 20)
	private String selfEvaluation;

	private Integer status;

	private Date createDate;

	private Date updateDate;

	private String resumeInfo;

	private String resumeFile;

	private int agent;

	public InvitationApply()
	{
	}

	public String getEmailOld()
	{
		return emailOld;
	}

	public void setEmailOld(String emailOld)
	{
		this.emailOld = emailOld;
	}

	public String getMobileOld()
	{
		return mobileOld;
	}

	public void setMobileOld(String mobileOld)
	{
		this.mobileOld = mobileOld;
	}

	public int getAgent()
	{
		return agent;
	}

	public void setAgent(int agent)
	{
		this.agent = agent;
	}

	public String getResumeInfo()
	{
		return resumeInfo;
	}

	public String getResumeFile()
	{
		return resumeFile;
	}

	public void setResumeFile(String resumeFile)
	{
		this.resumeFile = resumeFile;
	}

	public void setResumeInfo(String resumeInfo)
	{
		this.resumeInfo = resumeInfo;
	}

	public InvitationApply(Integer uid, String email, Integer invitationId, String jid)
	{
		this.uid = uid;
		this.email = email;
		this.invitationId = invitationId;
		this.jid = jid;
	}

	public Integer getUid()
	{
		return uid;
	}

	public void setUid(Integer uid)
	{
		this.uid = uid;
	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getMobile()
	{
		if (StringUtil.isEmpty(this.mobile))
		{
			this.mobile = "";
		}
		return mobile;
	}

	public void setMobile(String mobile)
	{
		this.mobile = mobile;
	}

	public Integer getInvitationId()
	{
		return invitationId;
	}

	public void setInvitationId(Integer invitationId)
	{
		this.invitationId = invitationId;
	}

	public String getJid()
	{
		return jid;
	}

	public void setJid(String jid)
	{
		this.jid = jid;
	}

	public BasicInfo getBasicInfo()
	{
		if (this.basicInfo == null)
		{
			basicInfo = new BasicInfo();
		}
		return basicInfo;
	}

	public void setBasicInfo(BasicInfo basicInfo)
	{
		this.basicInfo = basicInfo;
	}

	public PracticeInfo getDefaultPracticeInfo()
	{
		int flag = 0;
		if (this.getPracticeInfos().size() > 0)
		{
			for (int k = 0; k < this.getPracticeInfos().size(); k++)
			{
				if (this.getPracticeInfos().get(flag).getEndDate() != null
						&& this.getPracticeInfos().get(k).getEndDate() != null
						&& this.getPracticeInfos().get(flag).getEndDate()
								.before(this.getPracticeInfos().get(k).getEndDate()))
				{
					flag = k;
				}
			}
			this.defaultPracticeInfo = this.getPracticeInfos().get(flag);
		}
		if (this.defaultPracticeInfo == null)
		{
			this.defaultPracticeInfo = new PracticeInfo();
		}
		return defaultPracticeInfo;
	}

	public void setDefaultPracticeInfo(PracticeInfo defaultPracticeInfo)
	{
		this.defaultPracticeInfo = defaultPracticeInfo;
	}

	public EduInfo getDefaultEduInfo()
	{
		int flag = 0;
		if (this.getEduInfos().size() > 0)
		{
			for (int k = 0; k < this.getEduInfos().size(); k++)
			{
				if (this.getEduInfos().get(flag).getEndDate() != null
						&& this.getEduInfos().get(k).getEndDate() != null
						&& this.getEduInfos().get(flag).getEndDate()
								.before(this.getEduInfos().get(k).getEndDate()))
				{
					flag = k;
				}
			}
			this.defaultEduInfo = this.getEduInfos().get(flag);
		}
		if (this.defaultEduInfo == null)
		{
			this.defaultEduInfo = new EduInfo();
		}
		return defaultEduInfo;
	}

	public void setDefaultEduInfo(EduInfo defaultEduInfo)
	{
		this.defaultEduInfo = defaultEduInfo;
	}

	public IntentionInfo getIntentionInfo()
	{
		if (this.intentionInfo == null)
		{
			this.intentionInfo = new IntentionInfo();
		}
		return intentionInfo;
	}

	public void setIntentionInfo(IntentionInfo intentionInfo)
	{
		this.intentionInfo = intentionInfo;
	}

	public String getItskillsDesc()
	{
		return itskillsDesc;
	}

	public void setItskillsDesc(String itskillsDesc)
	{
		this.itskillsDesc = itskillsDesc;
	}

	public String getSelfEvaluation()
	{
		return selfEvaluation;
	}

	public void setSelfEvaluation(String selfEvaluation)
	{
		this.selfEvaluation = selfEvaluation;
	}

	public Integer getStatus()
	{
		return status;
	}

	public void setStatus(Integer status)
	{
		this.status = status;
	}

	public Date getCreateDate()
	{
		return createDate;
	}

	public void setCreateDate(Object createDate)
	{
		this.createDate = castToDate(createDate);
	}

	public Date getUpdateDate()
	{
		return updateDate;
	}

	public void setUpdateDate(Object updateDate)
	{
		this.updateDate = castToDate(updateDate);
	}

	public List<PracticeInfo> getPracticeInfos()
	{
		if (this.practiceInfos == null)
		{
			this.practiceInfos = new ArrayList<PracticeInfo>();
		}
		return practiceInfos;
	}

	public PracticeInfo getPracticeInfoById(Integer id)
	{
		if (this.getPracticeInfos().size() > 0)
		{
			PracticeInfo practice = null;
			for (PracticeInfo pinfo : this.getPracticeInfos())
			{
				if (pinfo.getId() == id)
				{
					practice = pinfo;
				}
			}
			return practice;
		} else
		{
			return null;
		}
	}

	public void deletePracticeInfoById(Integer id)
	{
		if (this.getPracticeInfos().size() > 0)
		{
			for (int idx = this.getPracticeInfos().size() - 1; idx >= 0; idx--)
			{
				if (this.getPracticeInfos().get(idx).getId() == id)
				{
					this.getPracticeInfos().remove(idx);
					continue;
				}
				// if (this.getPracticeInfos().get(idx).getEndDate() != null
				// && this.getDefaultPracticeInfo().getEndDate() != null
				// && this.getPracticeInfos().get(idx).getEndDate()
				// .after(this.getDefaultPracticeInfo().getEndDate()))
				// {
				// this.setDefaultPracticeInfo(this.getPracticeInfos().get(idx));
				// }
			}
		}
		if (this.getPracticeInfos().size() == 0)
		{
			this.setDefaultPracticeInfo(null);
		}
	}

	public void addPracticeInfo(PracticeInfo practiceInfo)
	{
		int maxId = 0;
		if (this.getPracticeInfos().size() > 0)
		{
			for (PracticeInfo pinfo : this.getPracticeInfos())
			{
				if (pinfo.equals(practiceInfo))
				{
					return;
				}
				if (pinfo.getId() > maxId)
				{
					maxId = pinfo.getId();
				}
			}
		} else
		{
			this.setDefaultPracticeInfo(practiceInfo);
		}

		if (this.getDefaultPracticeInfo().getEndDate() != null && this.getDefaultPracticeInfo().getEndDate() != null
				&& this.getDefaultPracticeInfo().getEndDate().before(practiceInfo.getEndDate()))
		{
			this.setDefaultPracticeInfo(practiceInfo);
		}
		practiceInfo.setId(maxId + 1);
		this.getPracticeInfos().add(practiceInfo);
	}

	public void setPracticeInfos(List<PracticeInfo> practiceInfos)
	{
		this.practiceInfos = practiceInfos;
	}

	public List<EduInfo> getEduInfos()
	{
		if (this.eduInfos == null)
		{
			this.eduInfos = new ArrayList<EduInfo>();
		}
		return eduInfos;
	}

	public EduInfo getEduInfoById(Integer id)
	{
		if (this.getEduInfos().size() > 0)
		{
			EduInfo eduInfo = null;
			for (EduInfo einfo : this.getEduInfos())
			{
				if (einfo.getId() == id)
				{
					eduInfo = einfo;
				}
			}
			return eduInfo;
		} else
		{
			return null;
		}
	}

	public void deleteEduInfoById(Integer id)
	{
		if (this.getEduInfos().size() > 0)
		{
			for (int idx = this.getEduInfos().size() - 1; idx >= 0; idx--)
			{
				if (this.getEduInfos().get(idx).getId() == id)
				{
					this.getEduInfos().remove(idx);
					continue;
				}
				// if (this.getEduInfos().get(idx).getEndDate() != null &&
				// this.getDefaultEduInfo().getEndDate() != null
				// &&
				// this.getEduInfos().get(idx).getEndDate().after(this.getDefaultEduInfo().getEndDate()))
				// {
				// this.setDefaultEduInfo(this.getEduInfos().get(idx));
				// }
			}
		}
		if (this.getEduInfos().size() == 0)
		{
			this.setDefaultEduInfo(null);
		}
	}

	public void addEduInfo(EduInfo eduInfo)
	{
		int maxId = 0;
		if (this.getEduInfos().size() > 0)
		{
			for (EduInfo einfo : this.getEduInfos())
			{
				if (einfo.equals(eduInfo))
				{
					return;
				}
				if (einfo.getId() > maxId)
				{
					maxId = einfo.getId();
				}
			}
		} else
		{
			this.setDefaultEduInfo(eduInfo);
		}

		if (this.getDefaultEduInfo().getEndDate() != null && eduInfo.getEndDate() != null
				&& this.getDefaultEduInfo().getEndDate().before(eduInfo.getEndDate()))
		{
			this.setDefaultEduInfo(eduInfo);
		}
		eduInfo.setId(maxId + 1);
		this.getEduInfos().add(eduInfo);
	}

	public void setEduInfos(List<EduInfo> eduInfos)
	{
		this.eduInfos = eduInfos;
	}

	public void resolveResumeInfo()
	{
		if (StringUtil.isNotEmpty(this.resumeInfo))
		{
			JSONObject iaJson = JSONObject.parseObject(this.resumeInfo);
			this.uid = iaJson.getIntValue("uid");
			this.email = iaJson.getString("email");
			this.mobile = iaJson.getString("mobile");
			this.agent = iaJson.getIntValue("agent");
			this.status = iaJson.getIntValue("status");
			this.createDate = iaJson.getDate("createDate");
			this.updateDate = iaJson.getDate("updateDate");
			this.jid = iaJson.getString("jid");
			this.itskillsDesc = iaJson.getString("itskillsDesc");
			this.selfEvaluation = iaJson.getString("selfEvaluation");
			this.resumeFile = iaJson.getString("resumeFile");
			resolveBasicInfo(this.getBasicInfo(), iaJson.getJSONObject("basicInfo"));
			resolvePractice(this.getPracticeInfos(), iaJson.getJSONObject("practiceInfos"));
		}
	}

	private void resolveBasicInfo(BasicInfo bi, JSONObject jo)
	{
		bi.setName(jo.getString("name"));
		bi.setGender(jo.getInteger("gender"));
		bi.setBirthday(jo.getDate("birthday"));
		bi.setLivecity(jo.getInteger("livecit"));
		bi.setDomicile(jo.getInteger("domicile"));
		bi.setQq(jo.getString("qq"));
		bi.setPhoto(jo.getString("photo"));
		bi.setWorkYears(jo.getInteger("workYears"));
		bi.setResumeAtta(jo.getString("resumeAtta"));
	}

	private void resolvePractice(List<PracticeInfo> pis, JSONObject jo)
	{
		JSONObject.toJavaObject(jo, InvitationApply.class);
		for (PracticeInfo pi : pis)
		{
			pi.setCorpName(jo.getString("corpName"));
			pi.setDesc(jo.getString("desc"));

		}
	}

	public int getPercent()
	{
		Integer score = 0;
		score += getWeight(this);
		score += getWeight(this.getBasicInfo());
		score += getWeight(this.getDefaultPracticeInfo());
		score += getWeight(this.getDefaultEduInfo());
		return score / 10;
	}

	private int getWeight(Object obj)
	{
		int score = 0;
		Field[] fields = obj.getClass().getDeclaredFields();
		for (Field f : fields)
		{
			Annotation an = f.getAnnotation(Weight.class);
			if (an != null)
			{
				try
				{
					String getMethod = "get" + f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1);
					Method method = obj.getClass().getDeclaredMethod(getMethod, null);
					Object value = method.invoke(obj, null);
					if (value != null && !"".equals(value) && !"Sun Dec 01 00:00:00 CST 3".equals(value.toString())
							&& !"0".equals(value.toString()))
					{
						score += ((Weight) an).value();
					}
				} catch (Exception e)
				{

				}
			}
		}
		return score;
	}

}
